package com.chuangjiang.wxpayx.wxpayx.controller;


import com.chuangjiang.wxpayx.wxpayx.util.ConfigUtil;
import com.chuangjiang.wxpayx.wxpayx.util.PayCommonUtil;
import com.chuangjiang.wxpayx.wxpayx.util.WxPayUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/pay")
public class WxPayController {

    @RequestMapping("/query")
    public ResponseEntity couponsConfirm(Model m, @RequestParam("totalFee") String totalFee) {
        //openid可通过微信高级接口oath2.0网页授权接口获取到用户信息,此接口本文中就不提供了，如果有需要，请留言。
        //m.addAttribute("openid", openid);
        //orderNo是你的商品订单号，自行生成的随机订单号，但是要保证随机性，不能有重复订单号。
        //m.addAttribute("orderNo", orderNo);

        String timeStamp = PayCommonUtil.create_timestamp();
        String nonceStr = PayCommonUtil.create_nonce_str();
        m.addAttribute("appid", ConfigUtil.APPID);
        m.addAttribute("timestamp", timeStamp);
        m.addAttribute("nonceStr", nonceStr);

        // WxPayUtil.unifiedorder("外卖下单",orderNo);

        // String userAgent = request.getHeader(“user-agent”);
        // char agent = userAgent.charAt(userAgent.indexOf(“MicroMessenger”)+15);
        // m.addAttribute(“agent”, new String(new char[]{agent}));//微信版本号，用于前面提到的判断用户手机微信的版本是否是5.0以上版本。

        Map packageData = WxPayUtil.unifiedorder("外卖下单", totalFee);
        SortedMap<Object, Object> signParams = new TreeMap<Object, Object>();
        signParams.put("appId", ConfigUtil.APPID);
        signParams.put("nonceStr", nonceStr);
        //signParams.put("package", packageData.get("prepay_id").toString());
        signParams.put("timeStamp", timeStamp);
        signParams.put("signType", "MD5");



        //String sign = PayCommonUtil.createSha1Sign(packageData.get("nonceStr").toString());
        m.addAttribute("paySign", packageData.get("sign"));
        m.addAttribute("signature", null);
        m.addAttribute("package", packageData);

        return ResponseEntity.ok(m);
    }


}
